//
//  HRVideoPlayerCell.m
//  Hear
//
//  Created by mac on 2017/6/21.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRVideoPlayerCell.h"
#import "HRDynamicInfoHeadView.h"
#import "HRDynamicCommentView.h"
#import "HRCoverImageModel.h"
#import "HRCoverTimeModel.h"
#import "NSMutableAttributedString+GDAttString.h"
#import "YBPopupMenu.h"
#import "HRUMShareTool.h"
#import "UIView+WebVideoCache.h"

@interface HRVideoPlayerCell ()<YBPopupMenuDelegate,JPVideoPlayerDelegate>
@property (nonatomic, strong)UIView *contView;

@property (nonatomic, strong)UIImageView *iconImageView;
@property (nonatomic, strong)UILabel *nickNameLabel;
@property (nonatomic, strong)UILabel *timeLabel;
@property (nonatomic, strong)UILabel *contentLabel;



@property (nonatomic, strong)HRDynamicToolView *dynamicToolView;
@property (nonatomic, strong)HRDynamicCommentView *commentView;
@property (nonatomic, strong)UIButton *moreButton;


// 个人主页
@property (nonatomic, strong)UILabel *lineLabel;
@property (nonatomic, strong)UIImageView *lockImageView;

@property (nonatomic, strong)UILabel *remendLabel;
@property (nonatomic, strong)UIImageView *backImageView;
@property (nonatomic, strong)UIImageView *cirleLabel;


@end
@implementation HRVideoPlayerCell

-(void)setDynamicLayout:(HRDunamicLayout *)dynamicLayout{
    WeakSelf
    self.contView.frame = CGRectMake(0, 0, kFullWidth, dynamicLayout.cellHeight);
    self.lockImageView.frame = dynamicLayout.lockImageViewRect;
//    self.lockImageView.image = [HRCoverImageModel OriginImage:[UIImage imageNamed:@"锁-"] scaleToSize:CGSizeMake(self.lockImageView.width, self.lockImageView.height)];
    
    [HRCoverImageModel OriginImage:[UIImage imageNamed:@"锁-"] scaleToSize:CGSizeMake(self.lockImageView.width, self.lockImageView.height) callBackOriginImage:^(UIImage *originImage) {
        weakSelf.lockImageView.image = originImage;
    }];
    
    self.lineLabel.frame = dynamicLayout.lineRect;
    
    self.cirleLabel.frame = dynamicLayout.circleRect;
    
    self.backImageView.frame = dynamicLayout.backImageViewRect;
    
    self.contentLabel.frame = dynamicLayout.contentRect;

    self.timeLabel.frame = dynamicLayout.timeRect;
    
    self.videoImageView.frame = dynamicLayout.videoPlayViewRect;
    
    self.contentLabel.attributedText = [NSMutableAttributedString attStringFromTextForChatting:dynamicLayout.dynamicModel.MomentNotes textFont:CustomUIFont(14)];
    self.timeLabel.text = [HRCoverTimeModel covetTime:dynamicLayout.dynamicModel.CreateTime];// 2017-04-13T17:20:11.29.1212

    
    if (!dynamicLayout.dynamicModel.Permission && ![dynamicLayout.dynamicModel.UserLocalId isEqualToString:USERID]){
        self.contentLabel.text = @"该动态上锁了，你想要钥匙吗";
    }
    
    if (!dynamicLayout.dynamicModel.Permission && [dynamicLayout.dynamicModel.UserLocalId isEqualToString:USERID]){
        self.remendLabel.frame = self.timeLabel.frame;
        self.remendLabel.width -= 15;
        self.remendLabel.text = @"自己可见";
    }
    
    
    [self.videoImageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?x-oss-process=image/resize,h_%d,w_%d",dynamicLayout.dynamicModel.VideoCover,(int)self.videoImageView.height,(int)self.videoImageView.width]] placeholder:[UIImage imageNamed:@"默认加载图"]];

    self.dynamicToolView.hidden = YES;
}



-(void)setLayout:(HRFollowDynamicLayout *)layout{
    _layout = layout;
    WeakSelf
    
    self.contView.frame = layout.contViewFrame;
    self.iconImageView.frame = layout.iconImageViewFrame;
    self.nickNameLabel.frame = layout.nickNameLabelFrame;
    self.timeLabel.frame = layout.timeLabelFrame;
    
    self.contentLabel.frame = layout.contentLabelFrame;
    self.moreButton.frame = layout.moreButtonFrame;
    
    self.commentView.frame = layout.commentViewFrame;
    self.dynamicToolView.frame = layout.dynamicToolViewFrame;

    
    self.videoImageView.frame = layout.playerViewFrame;
    
    
    HRDynamicModel *dynamicModel = layout.dynamicModel;
    
    
    if ([dynamicModel.UserLocalId isEqualToString:@"admin"]){
        weakSelf.iconImageView.image = [UIImage imageNamed:@"小助手"];
    }else{
//        self.iconImageView.image = [HRCoverImageModel OriginImage:[UIImage imageNamed:dynamicModel.UserPictureName] scaleToSize:CGSizeMake(40, 40)];
        
        [HRCoverImageModel OriginImage:[UIImage imageNamed:dynamicModel.UserPictureName] scaleToSize:CGSizeMake(40, 40) callBackOriginImage:^(UIImage *originImage) {
            weakSelf.iconImageView.image = originImage;
        }];
        
    }
    
    self.iconImageView.backgroundColor = [UIColor colorWithHexString:dynamicModel.UserPictureBackgroundColor];
    
    self.nickNameLabel.text = dynamicModel.NickName;
    
    self.timeLabel.text = [HRCoverTimeModel covetTime:dynamicModel.CreateTime];
    
    
    self.contentLabel.attributedText = [NSMutableAttributedString attStringFromTextForChatting:dynamicModel.MomentNotes textFont:CustomUIFont(14)];
    
    self.commentView.dynamicModel = dynamicModel;
    
    self.dynamicToolView.dynamicModel = dynamicModel;
    

    
    [self.videoImageView setImageWithURL:[NSURL URLWithString:dynamicModel.VideoCover] placeholder:[UIImage imageNamed:@"默认加载图"]];

    

}

+(instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *cellID = @"HRVideoPlayerCell";
    
    HRVideoPlayerCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if(!cell){

        cell = [[HRVideoPlayerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];

    }

    return cell;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor = CustomColor(240, 240, 240, 1);

        [self setupDynamicView];

    }
    return self;
}

- (void)setupDynamicView{
    
    WeakSelf
    
    _contView = [UIView new];
    _contView.width = kScreenWidth;
    _contView.height = 1;
    _contView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_contView];
    
    _backImageView = [UIImageView new];
    [_contView addSubview:_backImageView];
    _backImageView.userInteractionEnabled = YES;
    UIImage *backimage = [UIImage imageNamed:@"乐谱背景"];
    
    self.lockImageView = [UIImageView new];
    [_backImageView addSubview:self.lockImageView];
    

    
    UIImageView *iconImageView = [UIImageView new];
    iconImageView.contentMode = UIViewContentModeScaleAspectFill;
    iconImageView.clipsToBounds = YES;
    iconImageView.layer.cornerRadius = 25;
    [_contView addSubview:(self.iconImageView = iconImageView)];
    iconImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *iconTap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        if (weakSelf.clickIconBlock){
            weakSelf.clickIconBlock();
        }
    }];
    [iconImageView addGestureRecognizer:iconTap];
    
    self.nickNameLabel = [UILabel new];
    self.nickNameLabel.textColor = CustomColor(28, 190, 160, 1);
    self.nickNameLabel.font = CustomUIFont(16);
    [_contView addSubview:self.nickNameLabel];
    
    self.moreButton = [UIButton buttonWithType:0];
    [self.moreButton setImage:[UIImage imageNamed:@"更多"] forState:UIControlStateNormal];
    [self.moreButton addTarget:self action:@selector(moreButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [_contView addSubview:self.moreButton];
    
    _backImageView.image = [backimage stretchableImageWithLeftCapWidth:backimage.size.width*0.5 topCapHeight:backimage.size.width*0.8];

    

    
    _contentLabel = [UILabel new];
    _contentLabel.numberOfLines = 0;
    _contentLabel.textColor = [UIColor blackColor];
    [_backImageView addSubview:_contentLabel];
    _contentLabel.font = CustomUIFont(14);
    
    HRDynamicCommentView *commentView = [HRDynamicCommentView new];
    [_contView addSubview:(self.commentView = commentView)];
    
    
    _lineLabel = [UILabel new];
    _lineLabel.backgroundColor = CustomColor(214, 213, 209, 1);
    [_contView addSubview:_lineLabel];
    
    _cirleLabel = [UIImageView new];
    [_contView addSubview:_cirleLabel];
    _cirleLabel.image = [UIImage imageNamed:@"动态线上的点"];
    _cirleLabel.clipsToBounds = YES;
    
    _timeLabel = [UILabel new];
    _timeLabel.font = CustomUIFont(13);
    _timeLabel.textColor = CustomColor(180, 180, 180, 1);
    [_backImageView addSubview:_timeLabel];
    
    
    _remendLabel = [UILabel new];
    _remendLabel.font = CustomUIFont(13);
    _remendLabel.textColor = CustomColor(180, 180, 180, 1);
    [_backImageView addSubview:_remendLabel];
    _remendLabel.textAlignment = 2;
    
    
    
    self.videoImageView = [UIImageView new];
    self.videoImageView.contentMode = UIViewContentModeScaleAspectFill;
    self.videoImageView.clipsToBounds = YES;
    [_backImageView addSubview:self.videoImageView];
    self.videoImageView.jp_videoPlayerDelegate = self;
    
    HRDynamicToolView *dynamicToolView = [HRDynamicToolView new];
    [_contView addSubview:(self.dynamicToolView = dynamicToolView)];
    dynamicToolView.backgroundColor = [UIColor whiteColor];
    dynamicToolView.likeButtonClickBlock = ^(BOOL isLike){
        if (weakSelf.likeButtonClickBlock){
            weakSelf.likeButtonClickBlock(isLike);
        };
    };
}



- (void)moreButtonClick{
    [YBPopupMenu showRelyOnView:self.moreButton titles:@[@"转发",@"举报"] icons:nil menuWidth:100.f textColor:MainColor delegate:self];

}

#pragma -mark- YBPopupMenuDelegate
- (void)ybPopupMenuDidSelectedAtIndex:(NSInteger)index ybPopupMenu:(YBPopupMenu *)ybPopupMenu{
    if(index == 0){
        NSString *contentStr = @"";
        if (self.layout.dynamicModel.MomentNotes.length){
            contentStr = self.layout.dynamicModel.MomentNotes;
        }else{
            contentStr = @"我在Hear留下的瞬间";
        }
        
        
        [HRUMShareTool uMShareToolTitle:contentStr andContentStr:contentStr andImage:[UIImage imageNamed:@"hearicon"] andShareUrl:[NSString stringWithFormat:@"%@M/MomentShare/%@",ShareHeadUrl,self.layout.dynamicModel.LocalId]isSeccess:^(BOOL isSeccess) {
            
        }];
    }else{
        
    }
}
- (void)ybPopupMenuBeganShow{
    self.moreButton.imageView.transform = CGAffineTransformRotate(self.contView.transform, M_PI);
}

- (void)ybPopupMenuBeganDismiss{
    self.moreButton.imageView.transform = CGAffineTransformIdentity;
}

#pragma mark -----------------------------------------
#pragma mark JPVideoPlayerDelegate

-(BOOL)videoPlayerManager:(JPVideoPlayerManager *)videoPlayerManager shouldAutoReplayForURL:(NSURL *)videoURL{
    // do something here.
    return YES;
}

-(BOOL)videoPlayerManager:(JPVideoPlayerManager *)videoPlayerManager shouldDownloadVideoForURL:(NSURL *)videoURL{
    // do something here.
    return YES;
}



- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
